﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SmtpState.cs" company="Martin Woodward">
//   Copyright (c) 2010 All Rights Reserved, Martin Woodward
//
//   This source is subject to the Apache 2.0 License
//   Please see the License.txt file for more information.
//   All other rights reserved.
//
//   THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
//   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//   PARTICULAR PURPOSE.
// </copyright>
// <summary>
//   SMTP response container.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace nDumbster.smtp
{
    /// <summary> 
    /// SMTP server state.
    /// </summary>
    internal sealed class SmtpState
    {
        /// <summary>CONNECT state: waiting for a client connection. </summary>
        public static readonly SmtpState Connect = new SmtpState(CONNECT_BYTE);

        /// <summary>GREET state: waiting for a ELHO message. </summary>
        public static readonly SmtpState Greet = new SmtpState(GREET_BYTE);

        /// <summary>MAIL state: waiting for the MAIL FROM: command. </summary>
        public static readonly SmtpState Mail = new SmtpState(MAIL_BYTE);

        /// <summary>RCPT state: waiting for a RCPT &lt;email address&gt; command. </summary>
        public static readonly SmtpState Rcpt = new SmtpState(RCPT_BYTE);

        /// <summary>Waiting for headers. </summary>
        public static readonly SmtpState DataHeader = new SmtpState(DATA_HEADER_BYTE);

        /// <summary>Processing body text. </summary>
        public static readonly SmtpState DataBody = new SmtpState(DATA_BODY_BYTE);

        /// <summary>End of client transmission. </summary>
        public static readonly SmtpState Quit = new SmtpState(QUIT_BYTE);

        /// <summary>Internal representation of the CONNECT state. </summary>
        private const sbyte CONNECT_BYTE = 1;
        /// <summary>Internal representation of the GREET state. </summary>
        private const sbyte GREET_BYTE = 2;
        /// <summary>Internal representation of the MAIL state. </summary>
        private const sbyte MAIL_BYTE = 3;
        /// <summary>Internal representation of the RCPT state. </summary>
        private const sbyte RCPT_BYTE = 4;
        /// <summary>Internal representation of the DATA_HEADER state. </summary>
        private const sbyte DATA_HEADER_BYTE = 5;
        /// <summary>Internal representation of the DATA_BODY state. </summary>
        private const sbyte DATA_BODY_BYTE = 6;
        /// <summary>Internal representation of the QUIT state. </summary>
        private const sbyte QUIT_BYTE = 7;
        
        /// <summary>Internal representation of the state. </summary>
        private readonly sbyte state;

        /// <summary>
        /// Initializes a new instance of the <see cref="SmtpState"/> class. 
        /// Private to ensure that only valid states can be created.
        /// </summary>
        /// <param name="state">
        /// one of the _BYTE values.
        /// </param>
        private SmtpState(sbyte state)
        {
            this.state = state;
        }

        /// <summary>
        /// String representation of this SmtpState.
        /// </summary>
        /// <returns>A String that represents the current SmtpState</returns>
        public override string ToString()
        {
            switch (this.state)
            {

                case CONNECT_BYTE:
                    return "CONNECT";

                case GREET_BYTE:
                    return "GREET";

                case MAIL_BYTE:
                    return "MAIL";

                case RCPT_BYTE:
                    return "RCPT";

                case DATA_HEADER_BYTE:
                    return "DATA_HDR";

                case DATA_BODY_BYTE:
                    return "DATA_BODY";

                case QUIT_BYTE:
                    return "QUIT";

                default:
                    return "Unknown";
            }
        }
    }

}
